package net.jiastudy.biz.user.entity;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.validation.constraints.NotEmpty;

import com.alibaba.fastjson.annotation.JSONField;
import com.baomidou.mybatisplus.annotations.TableField;

import io.swagger.annotations.ApiModelProperty;
import net.jiastudy.biz.enums.RoleDataPermission;
import net.jiastudy.core.base.BaseEntity;

public class Role extends BaseEntity<Role> {

	/**
	 * 
	 */
	private static final long serialVersionUID = 5572414174763538414L;
	@ApiModelProperty(value = "角色名")
	@NotEmpty(message = "名称不能为空")
	private String name;
	@ApiModelProperty(value = "角色code")
	private String code;
	private Long createdUserId;
	private RoleDataPermission dataPermission;

	private Boolean sysRole;

	public RoleDataPermission getDataPermission() {
		return dataPermission;
	}

	public void setDataPermission(RoleDataPermission dataPermission) {
		this.dataPermission = dataPermission;
	}

	public Boolean isSysRole() {
		return sysRole;
	}

	public void setSysRole(boolean sysRole) {
		this.sysRole = sysRole;
	}

	@TableField(exist = false)
	private List<RolePermission> rolePermissions;

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getCode() {
		return code;
	}

	public void setCode(String code) {
		this.code = code;
	}

	@JSONField(serialize = false)
	public List<RolePermission> getRolePermissions() {
		return rolePermissions;
	}

	public void setRolePermissions(List<RolePermission> rolePermissions) {
		this.rolePermissions = rolePermissions;
	}

	public void setPermissions(Set<String> permissions) {
		if (null == permissions) {
			return;
		}
		if (null == this.rolePermissions) {
			this.rolePermissions = new ArrayList<>();
		}
		for (String permission : permissions) {
			RolePermission rolePermission = new RolePermission();
			rolePermission.setPermission(permission);
			rolePermission.setRole(this);
			this.rolePermissions.add(rolePermission);
		}
	}

	public Set<String> getPermissions() {
		if (null == this.rolePermissions) {
			return null;
		}
		Set<String> permissions = new HashSet<>();
		for (RolePermission rolePermission : this.rolePermissions) {
			permissions.add(rolePermission.getPermission());
		}
		return permissions;
	}

	public void addPermission(String permission) {
		if (null == permission) {
			return;
		}
		if (null == this.rolePermissions) {
			this.rolePermissions = new ArrayList<>();
		}
		RolePermission rolePermission = new RolePermission();
		rolePermission.setPermission(permission);
		rolePermission.setRole(this);
		this.rolePermissions.add(rolePermission);
	}

	public String getCreateUser() {
		if (null == createdUserId) {
			return null;
		}
		User user = new User().selectById(createdUserId);
		if (null == user) {
			return null;
		}
		return user.getName();
	}

	public Long getCreatedUserId() {
		return createdUserId;
	}

	public void setCreatedUserId(Long createdUserId) {
		this.createdUserId = createdUserId;
	}

}
